package com.wn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.bean.*;
import com.wn.service.IHouseService;
import com.wn.service.IOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.jws.Oneway;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellow Docter
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/owner")
public class OwnerController {

    @Autowired
    private IOwnerService iOwnerService;

    @Autowired
    private IHouseService houseService;

    /**
     * 分页查询:
     * 01  配置 分页插件
     *
     * 02 拿到前端的分页数据
     *
     * 03 创建 page对象 (currentPage,pageSize)
     *      IPage<Owner> page = new Page<>(currentPage,pageSize);
     * 04 调用分页方法
     *     IPage<Owner> housePage = iOwnerService.page(page);
     * 05 组装数据返回结果
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping
    public R queryHouse(Integer currentPage,Integer pageSize,String tel,String identity){

        IPage<Owner> page = new Page<>(currentPage,pageSize);

        //条件查询
        QueryWrapper<Owner> qr = new QueryWrapper<>();

        /**
         *
         *  mybatisPlus 默认的 多个条件查询是 and关系
         *
         *  查询的多个条件 一般是:or关系
         *
         */
        if (StringUtils.hasLength(tel)){
            qr.eq("tel",tel);
        }

        if (StringUtils.hasLength(identity)){
            qr.or().eq("identity",identity);
        }

        IPage<Owner> ownerPage = iOwnerService.page(page,qr);

        List<Owner> records = ownerPage.getRecords();

        records.forEach(ow->{

            House house = houseService.getById(ow.getHouseId());
            ow.setHouse(house);
        });

        Long total = ownerPage.getTotal();

        // 组装数据返回结果
        R r = new R();

        if (!CollectionUtils.isEmpty(records)){
            PageBean<Owner> pageBean = new PageBean<>();
            pageBean.setData(records);
            pageBean.setTotal(total.intValue());

            r.setData(pageBean);
            r.setMsg("查询成功");
            r.setCode(Constants.SUCCESS_CODE);

            return r;
        }
        return R.ERROR("查询失败");
    }

    @PostMapping
    public R addOwner(@RequestBody Owner owner){

        boolean save = iOwnerService.save(owner);

        if (save){
            return R.OK("添加成功...");
        }
        return R.ERROR();
    }

    @PutMapping
    public R updateOwner(@RequestBody Owner owner){

        boolean save = iOwnerService.updateById(owner);

        if (save){
            return R.OK("修改成功...");
        }
        return R.ERROR();
    }

    @GetMapping("/all")
    public R queryOwnerAll(){

        List<Owner> list = iOwnerService.list();

        // 组装数据返回结果
        R r = new R();
        if (!CollectionUtils.isEmpty(list)){
            r.setData(list);
            r.setMsg("查询成功");
            r.setCode(Constants.SUCCESS_CODE);
            return r;
        }
        return R.ERROR("查询失败");
    }


}

